home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / ddj0897.zip / DYN401.ZIP / utils / win32 / rm.c < prev    next >
C/C++ Source or Header  |  1995-08-27  |  3KB  |  172 lines

  1.  
  2.  
  3. /*
  4.  *
  5.  *    Copyright 1993 Algorithms Corporation
  6.  *
  7.  *    ALL RIGHTS RESERVED.
  8.  *
  9.  *
  10.  *
  11.  */
  12.  
  13.  
  14.  
  15.  
  16. #include <stdio.h> 
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #include <dos.h>
  20. #include <io.h>
  21. #include <windows.h>
  22.  
  23. static    void    strip_path();
  24. static    void    usage();
  25. static    int    rm(), erase_dir();;
  26.  
  27.  
  28. static    int    verbose = 0;
  29.  
  30.  
  31. int    main(argc, argv)
  32. int    argc;
  33. char    *argv[];
  34. {
  35.     int    r, i, isdir, err=0, force=0, quiet=0, recur=0, zero=0;
  36.     struct    stat    sb;
  37.     
  38.     if (argc < 2)
  39.         usage();
  40.     for (i=1 ; i < argc ; ++i)  {
  41.         if (argv[i][0] == '-')  {
  42.             for (r=1 ; argv[i][r] ; ++r)
  43.                 switch (argv[i][r])  {
  44.                 case 'f':
  45.                 case 'F':
  46.                     force = 1;
  47.                     break;
  48.                 case 'q':
  49.                 case 'Q':
  50.                     quiet = 1;
  51.                     verbose = 0;
  52.                     break;
  53.                 case 'r':
  54.                 case 'R':
  55.                     recur = 1;
  56.                     break;
  57.                 case 'v':
  58.                 case 'V':
  59.                     verbose = 1;
  60.                     quiet = 0;
  61.                     break;
  62.                 case 'z':
  63.                 case 'Z':
  64.                     zero = 1;
  65.                     break;
  66.                 default:
  67.                     fprintf(stderr, "rm:  Unknown flag %c\n", argv[i][r]);
  68.                     usage();
  69.                     break;
  70.                 }
  71.             continue;
  72.         }
  73.         r = stat(argv[i], &sb);
  74.         if (!r  &&  recur  &&  (sb.st_mode & S_IFDIR))
  75.             err |= erase_dir(argv[i], force, quiet);
  76.         else
  77.             err |= rm(argv[i], force, quiet);
  78.     }
  79.     return zero ? 0 : err;
  80. }
  81.  
  82. static    void    usage()
  83. {
  84.     printf("Usage:\trm  [-option]  file...\n"); 
  85.     printf("Options:\n");
  86.     printf("\tf\tforce (good for read only files)\n");
  87.     printf("\tq\tquiet (no error messages)\n");
  88.     printf("\tr\trecurse sub-directories\n");
  89.     printf("\tv\tverbose\n");
  90.     printf("\tz\talways return 0\n");
  91.     exit(1); 
  92. }
  93.  
  94. static    void    strip_path(to, from)
  95. char    *from, *to;
  96. {
  97.     char    *t;
  98.  
  99.     for (t=from ; *t ; ++t)
  100.         if (*t == '/'  ||  *t == '\\'  ||  *t == ':')
  101.             from = t + 1;
  102.     strcpy(to, from);
  103. }
  104.  
  105. static    int    rm(f, force, quiet)
  106. char    *f;
  107. int    force, quiet;
  108. {
  109.     int    r;
  110.  
  111.     if (verbose)
  112.         fprintf(stderr, "Removing %s\n", f);
  113.     r = unlink(f);
  114.     if (r  &&  force)  {
  115.         SetFileAttributes(f, FILE_ATTRIBUTE_NORMAL);
  116.         r = unlink(f);
  117.     }
  118.     if (r  &&  !quiet)
  119.         fprintf(stderr, "rm:  Can't remove %s\n", f);
  120.     return(r);
  121. }
  122.  
  123. static    int    erase_dir(d, force, quiet)
  124. char    *d;
  125. int    force, quiet;
  126. {
  127.     char    path[80];
  128.     struct    stat    statbuf;
  129.     struct    _finddata_t    dir;
  130.     int    err=0, r;
  131.     long    fh;
  132.     
  133.     sprintf(path, "%s/*", d);
  134.     if (-1L == (fh = _findfirst(path, &dir)))
  135.         goto end;
  136.     do {
  137.         if (strcmp(dir.name, ".")  &&  strcmp(dir.name, ".."))  {
  138.             sprintf(path, "%s/%s", d, dir.name);
  139.             stat(path, &statbuf);
  140.             if (statbuf.st_mode & S_IFDIR)  /*  its a directory    */
  141.                 err |= erase_dir(path, force, quiet);
  142.             else
  143.                 err |= rm(path, force, quiet);
  144.         }
  145.     }  while  (!_findnext(fh, &dir));
  146.     _findclose(fh);
  147.  end:
  148.     if (strcmp(d, ".")  &&  strcmp(d, "..")  &&
  149.         strcmp(d, "/")  &&  strcmp(d, "\\"))  {
  150.         r = rmdir(d);
  151.         if (r  &&  !quiet)
  152.             fprintf(stderr, "rm:  Can't remove directory %s\n", d);
  153.         return r;
  154.     }  else
  155.         return 0;
  156. }
  157.  
  158.  
  159.  
  160.  
  161. /*
  162.  *
  163.  *    Copyright 1993 Algorithms Corporation
  164.  *
  165.  *    ALL RIGHTS RESERVED.
  166.  *
  167.  *
  168.  *
  169.  */
  170.  
  171.  
  172.